const Router = require('koa-router')
const Poi = require('../dbs/models/poi')
const Shop = require('../dbs/models/shop')

const router = new Router({ prefix: '/search' })

router.get('/top', async (ctx) => {
  try {
    const top = await Poi.find({
      'name': new RegExp(ctx.query.input),
      city: ctx.query.city
    })
    ctx.body = {
      code: 0,
      top: top.map((item) => {
        return {
          name: item.name,
          type: item.type
        }
      }),
      type: top.length ? top[0].type : ''
    }
  } catch (e) {
    ctx.body = {
      code: -1,
      top: []
    }
  }
})

router.get('/hotPlace', async (ctx) => {
  const city = ctx.store ? ctx.store.geo.position.city : ctx.query.city
  try {
    const result = await Shop.find({
      city
    }).limit(3)

    ctx.body = {
      code: 0,
      result: result.map((item) => {
        return {
          name: item.name,
          type: item.type
        }
      })
    }
  } catch (e) {
    ctx.body = {
      code: -1,
      result: []
    }
  }
})
router.get('/resultsByName', async (ctx) => {
  const city = ctx.query.city || '北京市'
  const name = ctx.query.name || ''
  try {
    let result = []
    if (name) {
      result = await Shop.find({
        city,
        name: new RegExp(name, 'i')
      })
    }
    const pois = result.map((item) => {
      return {
        name: item.name,
        tel: item.tel,
        address: item.address,
        tag: item.tag,
        type: item.type,
        photos: item.photos,
        biz_ext: {
          cost: item.cost,
          rating: item.rating
        }
      }
    })
    ctx.body = {
      code: 0,
      count: result.length,
      pois
    }
  } catch (e) {
    ctx.body = {
      code: -1,
      pois: [],
      msg: e
    }
  }
})
router.get('/resultsByKeywords', async (ctx) => {
  const type = ctx.query.keyword || ''
  const city = ctx.query.city || '北京市'
  try {
    let result = []
    if (type) {
      result = await Shop.find({
        city,
        type
      })
    } else {
      result = await Shop.find({
        city
      })
    }
    const pois = result.map((item) => {
      return {
        name: item.name,
        tel: item.tel,
        address: item.address,
        tag: item.tag,
        type: item.type,
        photos: item.photos,
        biz_ext: {
          cost: item.cost,
          rating: item.rating
        }
      }
    })
    ctx.body = {
      code: 0,
      count: result.length,
      pois
    }
  } catch (e) {
    ctx.body = {
      code: -1,
      pois: [],
      msg: e
    }
  }
})

router.get('/products', async (ctx) => {
  const name = ctx.query.keyword
  const city = ctx.query.city
  const more = product
  try {
    const result = await Shop.find({
      city,
      name
    })
    const pois = result.map((item) => {
      return {
        name: item.name,
        tel: item.tel,
        address: item.address,
        tag: item.tag,
        type: item.type,
        photos: item.photos,
        biz_ext: {
          cost: item.cost,
          rating: item.rating
        }
      }
    })
    ctx.body = {
      code: 0,
      product: pois[0],
      more: ctx.isAuthenticated() ? more : [],
      login: ctx.isAuthenticated()
    }
  } catch (e) {
    ctx.body = {
      code: -1,
      msg: 'db error',
      product: {},
      more: ctx.isAuthenticated() ? more : [],
      login: ctx.isAuthenticated()
    }
  }
})

const product = [{
  name: '商品一',
  tag: '暂无描述',
  type: '学校周边',
  deadline: '2020.9.3',
  biz_ext: { cost: '128', rating: 3, ticket_ordering: 99 },
  photos: ['https://p1.meituan.net/msmerchant/be30be2f637550f623f014027c139fd0695848.jpg@460w_260h_1e_1c']
}, {
  name: '商品二',
  tag: '暂无描述',
  type: '亚龙湾百花谷商业街',
  deadline: '2020.9.3',
  biz_ext: { cost: '228', rating: 4, ticket_ordering: 4 },
  photos: ['https://p1.meituan.net/msmerchant/de8a433be44cb36088b170490d6173db323302.jpg@460w_260h_1e_1c']
}]

// export default router
module.exports = router
